package br.usp.icmc.biomac.client.activity;

import java.util.ArrayList;
import java.util.List;

import br.usp.icmc.biomac.client.ClientFactory;
import br.usp.icmc.biomac.client.place.ImageListPlace;
import br.usp.icmc.biomac.client.view.DicomResultCellBrowser;
import br.usp.icmc.biomac.client.view.DicomResultCellTable;
import br.usp.icmc.biomac.client.view.DicomSearchWidget;
import br.usp.icmc.biomac.client.view.ImageAnnotation;
import br.usp.icmc.biomac.client.view.ImageListView;
import br.usp.icmc.biomac.client.view.ImageView;
import br.usp.icmc.biomac.client.view.ROIAnnotationCellTable;
import br.usp.icmc.biomac.client.view.ROIImageCellTable;
import br.usp.icmc.biomac.client.view.ThumbnailCell;
import br.usp.icmc.biomac.shared.CSVData;
import br.usp.icmc.biomac.shared.CSVSerie;
import br.usp.icmc.biomac.shared.ImageDicom;
import br.usp.icmc.biomac.shared.ROI;
import br.usp.icmc.dilvan.aimTemplateViewer.client.AIMTemplateParser;
import br.usp.icmc.dilvan.aimTemplateViewer.client.ComponentsAIM;
import br.usp.icmc.dilvan.aimTemplateViewer.client.aim.ImageAnnotationAIM;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class ImageListActivity extends AbstractActivity implements
		DicomSearchWidget.Presenter, 
		ThumbnailCell.Presenter,
		ImageView.Presenter, 
		ImageAnnotation.Presenter,
		DicomResultCellBrowser.Presenter,
		DicomResultCellTable.Presenter,
		ROIImageCellTable.Presenter,
		ROIAnnotationCellTable.Presenter,
		ComponentsAIM.Presenter{

	ClientFactory clientFactory;
	
	ImageListPlace place;
	
	private int indexImage;
	private static boolean useBrowser = true;
	
	public ImageListActivity(ImageListPlace place, ClientFactory factory) {
		this.place = place;
		this.clientFactory = factory;
	}
	
	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		final ImageListView view = clientFactory.getImageListView();
		view.setPresenter(this);
		view.setImageView(clientFactory.getImageView());
		containerWidget.setWidget(view.asWidget());
		clientFactory.getRpcService().getImages("", new AsyncCallback<ArrayList<ImageDicom>>() {
			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(ArrayList<ImageDicom> result) {
				clientFactory.setImageDicom(result);
			}
		});
	}
	
	/**
	 * DicomSearchWidget.Presenter implemented methods 
	 */
	
	@Override
	public void setSearch(String parameter, String value) {
		setSearchRPC(parameter, value);
	}

	public void setSearchRPC(String parameter, String value) {
		final DicomResultCellBrowser viewBrowser = clientFactory.getDicomResultCellBrowser();
		final DicomResultCellTable viewTable = clientFactory.getDicomResultCellTable();

		viewBrowser.setPresenter(this);
		viewTable.setPresenter(this);

		// containerWidget.setWidget(view.asWidget());
		clientFactory.getRpcService().getCSVData(parameter, value,
				new AsyncCallback<ArrayList<CSVData>>() {
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error fetching images");
					}
					@Override
					public void onSuccess(ArrayList<CSVData> result) {
						if (useBrowser) {
							viewBrowser.setBrowserResult(result);
						} else {
							viewTable.setDataResult(result);
						}
						
					}
				});

	}
	
	/**
	 * ThumbnailCell.Presenter implemented methods 
	 */

	@Override
	public void setImagesLoad(String studyUID) {
		final ImageListView view = clientFactory.getImageListView();
		view.setPresenter(this);
		view.startDicomView();
	}
	
	/**
	 * ImageView.Presenter implemented methods 
	 */

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}
	
	/**
	 * ImageAnnotation.Presenter implemented methods 
	 */

	@Override
	public void setROIs() {
		// TODO Auto-generated method stub
	}


	/**
	 * Templates methods
	 */

	@Override
	public void getTemplateContainer() {
		final ImageAnnotation view = clientFactory.getImageAnnotation();
		view.setPresenter(this);
		view.setTemplatesContainers(AIMTemplateParser.getTemplateContainers());
	}

	@Override
	public void getTemplates(String container) {
		final ImageAnnotation view = clientFactory.getImageAnnotation();
		view.setPresenter(this);
		view.setTemplates(AIMTemplateParser.getShortTemplates(container));
	}

	@Override
	public void getTemplate(String container, String template) {
		final ImageAnnotation view = clientFactory.getImageAnnotation();
		view.setPresenter(this);
		view.createForm(AIMTemplateParser.getTemplate(container, template));
	}
	
	/**
	 * DicomResultCellTable.Presenter implemented methods 
	 */
	
	@Override
	public void loadThubmanais(String studyUID) {
		final ImageListView view = clientFactory.getImageListView();
		view.setPresenter(this);
		clientFactory.getRpcService().getCSVSerie(studyUID, new AsyncCallback<ArrayList<CSVSerie>>() {
			@Override
			public void onSuccess(ArrayList<CSVSerie> result) {
				view.loadThubmanais(result);
			}
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error fetching Series");
			}
		});
		
	}

	@Override
	public void goToAnnotationTab(ROI roi) {
		final ImageAnnotation view = clientFactory.getImageAnnotation();
		view.setPresenter(this);
		view.getTabs().selectTab(0);
		view.setROI(roi);
	}

	@Override
	public void showImageAnnotation(List<ROI> rois) {
		final ImageAnnotation view = clientFactory.getImageAnnotation();
		view.setPresenter(this);
		view.showAnnotation();
	}

	@Override
	public void addROI(final int indexImage, final ROI roi) {
		clientFactory.getRpcService().insertROI(indexImage, roi, new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				clientFactory.getImageDicom().get(indexImage).getRois().add(roi);
				clientFactory.getImageView().drawImage();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("The ROI can not create on the Server");
			}
		});
	}

	@Override
	public void rmvROI(final int indexImage, final int indexROI) {
		clientFactory.getRpcService().removeROI(indexImage, indexROI, new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				clientFactory.getImageDicom().get(indexImage).getRois().remove(indexROI);
				clientFactory.getImageView().drawImage();
			}
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("The ROI can not remove on the Server");
			}
		});
	}

	@Override
	public void setImages(ArrayList<ImageDicom> images) {
		clientFactory.setImageDicom(images);
	}

	@Override
	public ArrayList<ImageDicom> getImages() {
		return clientFactory.getImageDicom();
	}

	@Override
	public void getImages(String series) {
		clientFactory.getRpcService().getImages(series, new AsyncCallback<ArrayList<ImageDicom>>() {
			@Override
			public void onSuccess(ArrayList<ImageDicom> result) {
				clientFactory.setImageDicom(result);
			}
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Erro");
			}
		}); //return clientFactory.getImageDicom();
	}

	@Override
	public void edtROI(final int indexImage, final int indexROI, final String label) {
		clientFactory.getRpcService().editROI(indexImage, indexROI, label, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("The ROI can not edit on the Server");
			}
			@Override
			public void onSuccess(Boolean result) {
				clientFactory.getImageDicom().get(indexImage).getRois().get(indexROI).setLabel(label);
				//Window.alert(label);
			}
		});
	}

	@Override
	public int getIndexImage() {
		return indexImage;
	}

	@Override
	public void setIndexImage(int index) {
		indexImage = index;
	}

	@Override
	public void loadBrowserResult(String studyUID) {
		GWT.log("loadBrowserResult");
		// load the series selected in the browser dialog
		final ImageListView view = clientFactory.getImageListView();
		view.setPresenter(this);
		view.startDicomView();
		
	}
	

	public static boolean getBrowser() { 
		return(useBrowser);
		
	}

	public static void setBrowser(boolean setting) { 
		useBrowser = setting;
		
	}

	@Override
	public void sendResponseForm(ImageAnnotationAIM result) {
		// TODO Save the result from the template form
		clientFactory.getRpcService().saveAIM(result, new AsyncCallback<String>() {
			@Override
			public void onSuccess(String result) {
				// TODO Auto-generated method stub
				Window.alert(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("The annotation AIM was not save with sucessful!");
			}
		});
		// + result.getResponseTemplate().get(0).getLabel());
		//GWT.log("Saved AIM Activity");
	}

}
